package goulu.bloggy.com.Activities;

import java.util.ArrayList;

import goulu.bloggy.com.R;
import goulu.bloggy.com.Connections.PostMysqlConnection;
import goulu.bloggy.com.Helpers.PostHelper;
import goulu.bloggy.com.Models.Post;
import goulu.bloggy.com.Models.User;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class NewPostActivity extends Activity implements OnClickListener {
    
	EditText title;
    EditText content;
    TextView moodText;
    CheckBox published;
    User currentUser;
    String mood;
    ImageView moodImage;
    Dialog dialog;
    
    Handler postHandler = new Handler()
    {

		@Override
		public void handleMessage(Message msg) 
		{
			if(msg.what == PostMysqlConnection.ADD_POST)
			{
				Toast.makeText(getApplicationContext(), msg.obj.toString(), Toast.LENGTH_LONG).show();
				setResult(PostHelper.CONTENT_CHANGED);
				finish();
			}
		}
    	
    };
	
    //listeneri mood dialogin mood napeille
    public OnClickListener moodListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.moodImageButtonPlain:
				mood = "plain";
				break;
			case R.id.moodImageButtonHappy:
				mood = "happy";
				break;
			case R.id.moodImageButtonEmbarrassed:
				mood = "embarrassed";
				break;
			case R.id.moodImageButtonJoy:
				mood = "joy";
				break;
			case R.id.moodImageButtonLaugh:
				mood = "laugh";
				break;
			case R.id.moodImageButtonSad:
				mood = "sad";
				break;
			default:
				mood = null;
				break;
			}
			if(mood != null) {
				moodText.setText(mood);
				moodImage.setImageResource(PostHelper.getMoodResourceId(mood));
				dialog.dismiss();
			}
		}
	};
	
	//listeneri mood list dialogin cancel napille
	public OnClickListener moodListListener = new OnClickListener() {
		
		public void onClick(View v) {
			if(v.getId() == R.id.moodListButtonCancel) {
				mood = null;
				moodText.setText("None");
				moodImage.setImageResource(0);
				dialog.cancel();
			}
		}
	};
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newpostview);
        currentUser = (User)getIntent().getSerializableExtra("user");
        setupView();
        
    }
    
    public void setupView() {
    	title = (EditText)findViewById(R.id.newpostPostTitle);
    	content = (EditText)findViewById(R.id.newpostPostContent);
    	moodText = (TextView)findViewById(R.id.newpostMoodText);
    	moodText.setText("None");
    	published = (CheckBox)findViewById(R.id.newpostPublishedBox);
    	moodImage = (ImageView)findViewById(R.id.newpostMoodImage);
    	
    	Button moodButton = (Button)findViewById(R.id.newpostMoodButton);
    	moodButton.setOnClickListener(this);
    	Button cancelButton = (Button)findViewById(R.id.newpostCancelButton);
    	cancelButton.setOnClickListener(this);
    	Button saveButton = (Button)findViewById(R.id.newpostSaveButton);
    	saveButton.setOnClickListener(this);
    	
    }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.newpostMoodButton:
			showMoodDialog();
			break;

		case R.id.newpostCancelButton:
			setResult(PostHelper.NOTHING_CHANGED);
			finish();
			break;
		
		case R.id.newpostSaveButton:
			saveBlogPost();
			break;
		}
		
	}
	
	private void saveBlogPost() 
	{
		Post blogpost = new Post();
		blogpost.setTitle(title.getText().toString());
		blogpost.setContent(content.getText().toString());
		blogpost.setMood(moodText.getText().toString());
		blogpost.setPublished(published.isChecked());
		blogpost.setUserid(currentUser.getId());
		PostMysqlConnection conn = new PostMysqlConnection(postHandler, PostMysqlConnection.ADD_POST, blogpost);
		Thread t = new Thread(conn);
		t.start();
	}

	//Luo dialogin
	public void showMoodDialog() {
		
		dialog = new Dialog(this);
		
		dialog.setContentView(R.layout.listmoodview);
		dialog.setTitle("Select mood");
		dialog.setCancelable(true);

		ImageButton imgButtonPlain = (ImageButton)dialog.findViewById(R.id.moodImageButtonPlain);
		imgButtonPlain.setOnClickListener(moodListener);
		ImageButton imgButtonHappy = (ImageButton)dialog.findViewById(R.id.moodImageButtonHappy);
		imgButtonHappy.setOnClickListener(moodListener);
		ImageButton imgButtonEmbarrassed = (ImageButton)dialog.findViewById(R.id.moodImageButtonEmbarrassed);
		imgButtonEmbarrassed.setOnClickListener(moodListener);
		ImageButton imgButtonJoy = (ImageButton)dialog.findViewById(R.id.moodImageButtonJoy);
		imgButtonJoy.setOnClickListener(moodListener);
		ImageButton imgButtonLaugh = (ImageButton)dialog.findViewById(R.id.moodImageButtonLaugh);
		imgButtonLaugh.setOnClickListener(moodListener);
		ImageButton imgButtonSad = (ImageButton)dialog.findViewById(R.id.moodImageButtonSad);
		imgButtonSad.setOnClickListener(moodListener);
		Button moodButtonCancel = (Button)dialog.findViewById(R.id.moodListButtonCancel);
		moodButtonCancel.setOnClickListener(moodListListener);
		
		dialog.show();
	}
}